package com.ruoyi.system.service.impl;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.system.domain.TMiStock;
import com.ruoyi.system.domain.UpdateStatusRequest;
import com.ruoyi.system.mapper.TMiStockMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TBaseStorageAreaLocationMapper;
import com.ruoyi.system.domain.TBaseStorageAreaLocation;
import com.ruoyi.system.service.ITBaseStorageAreaLocationService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 库位管理Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-05
 */
@Service
public class TBaseStorageAreaLocationServiceImpl implements ITBaseStorageAreaLocationService
{
    @Autowired
    private TBaseStorageAreaLocationMapper tBaseStorageAreaLocationMapper;
    @Autowired
    private TMiStockMapper tMiStockMapper;
    /**
     * 查询库位管理
     *
     * @param locationId 库位管理主键
     * @return 库位管理
     */
    @Override
    public TBaseStorageAreaLocation selectTBaseStorageAreaLocationByLocationId(String locationId)
    {
        return tBaseStorageAreaLocationMapper.selectTBaseStorageAreaLocationByLocationId(locationId);
    }

    /**
     * 查询库位管理列表
     *
     * @param tBaseStorageAreaLocation 库位管理
     * @return 库位管理
     */
    @Override
    public List<TBaseStorageAreaLocation> selectTBaseStorageAreaLocationList(TBaseStorageAreaLocation tBaseStorageAreaLocation)
    {
        return tBaseStorageAreaLocationMapper.selectTBaseStorageAreaLocationList(tBaseStorageAreaLocation);
    }

    /**
     * 新增库位管理
     *
     * @param tBaseStorageAreaLocation 库位管理
     * @return 结果
     */
    @Override
    public int insertTBaseStorageAreaLocation(TBaseStorageAreaLocation tBaseStorageAreaLocation)
    {
        return tBaseStorageAreaLocationMapper.insertTBaseStorageAreaLocation(tBaseStorageAreaLocation);
    }

    /**
     * 修改库位管理
     *
     * @param tBaseStorageAreaLocation 库位管理
     * @return 结果
     */
    @Override
    public int updateTBaseStorageAreaLocation(TBaseStorageAreaLocation tBaseStorageAreaLocation)
    {
        return tBaseStorageAreaLocationMapper.updateTBaseStorageAreaLocation(tBaseStorageAreaLocation);
    }

    /**
     * 批量删除库位管理
     *
     * @param locationIds 需要删除的库位管理主键
     * @return 结果
     */
    @Override
    public int deleteTBaseStorageAreaLocationByLocationIds(String[] locationIds)
    {
        return tBaseStorageAreaLocationMapper.deleteTBaseStorageAreaLocationByLocationIds(locationIds);
    }

    /**
     * 删除库位管理信息
     *
     * @param locationId 库位管理主键
     * @return 结果
     */
    @Override
    public int deleteTBaseStorageAreaLocationByLocationId(String locationId)
    {
        return tBaseStorageAreaLocationMapper.deleteTBaseStorageAreaLocationByLocationId(locationId);
    }

    @Override

    public List<TBaseStorageAreaLocation> selectTBaseStorageAreaLocationByDecId(TBaseStorageAreaLocation tBaseStorageAreaLocation) {
        List<TBaseStorageAreaLocation> list = tBaseStorageAreaLocationMapper.selectTBaseStorageAreaLocationByDecId(tBaseStorageAreaLocation.getDecId());

//        for (TBaseStorageAreaLocation baseStorageAreaLocation : list) {
//            String locationId = baseStorageAreaLocation.getLocationId();
//            List<TMiStock> tMiStocks = tMiStockMapper.selectTMiStocksByLocationId(locationId);
//            int sts = tMiStocks.isEmpty() ? 0 : 1;
//            tBaseStorageAreaLocationMapper.setSTS(sts, locationId);
//
//        }

        return list;
    }

    @Override
    public List<TBaseStorageAreaLocation> selectByLocationId(String locationId) {
        return tBaseStorageAreaLocationMapper.selectByLocationId(locationId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateStatus(UpdateStatusRequest request) {
        List<String> locationIds = request.getLocationId();
        for (String locationId : locationIds) {
            List<TBaseStorageAreaLocation> tBaseStorageAreaLocations = tBaseStorageAreaLocationMapper.selectByLocationId(locationId);
            for (TBaseStorageAreaLocation tBaseStorageAreaLocation : tBaseStorageAreaLocations) {
                if (tBaseStorageAreaLocation.getSTATUS().equals("2")){
                    throw new RuntimeException("该库位已有库存");
                }
            }
        }
        return tBaseStorageAreaLocationMapper.updateStatus(request);
    }
}
